<?php


namespace app\admin\model;

use think\Db;

class ChangeModel extends BaseModel
{
    protected $name='change';

    protected $pk='id';
    protected $autoWriteTimestamp = true;
    protected $updateTime = false;
    protected $createTime = 'add_time';

    public  function doAddData($data=[],$mw=[]){
        $this->startTrans();
        try {
            $change = $data;
            $change['change_time'] = strtotime($change['change_time']);
            $change['order_id'] = $mw['order_id'];
            $change['tomb_id'] = $mw['tomb_id'];
            $change['customer_id'] = $mw['customer_id'];
            $change['add_user'] = cmf_get_current_admin_id();
            $changes = self::create($change);
            $change_id = $changes['id'];

            // 如果提交审核，则往审核表查数据
            if($change['status'] == 1){
                // 往审批表插数据
                $detail_type = 6;// 挂失
                if($change['type'] == 1){
                    // 变更
                    $detail_type = 5;
                }
                $pay_ins = [
                    'form_type'=>2,//2 业务
                    'detail_type'=>$detail_type,//5 证件变更 6证件挂失
                    'apply_user'=>cmf_get_current_admin_id(),//申请人
                    'apply_time'=>time(),//申请时间
                    'approval_status'=>1,// 表单状态 未审核
                    'form_id'=>$change_id,//表单ID
                    'order_id'=>$mw['order_id'],
                ];
                Db::name('approval')->insertGetId($pay_ins);
            }

            $result = $change_id;
            $this->commit();
        }catch(\Exception $e){
            $this->error='操作失败';
            $result = false;
            $this->rollback();
        }
        return $result;
    }
}